<?php
namespace app\admin\controller;

use app\admin\model\MemberModel;
use app\admin\logic\MemberLogic;
use Session;

class Member extends Base
{

    public function index()
    {
        $member  = new MemberModel();

        $type     = input("param.type/d");
        $pagenum  = input("param.page/d") ;
        $pagesize = input("param.limit/d");
        $status   = input("param.status/d");

        if ($type == 1) {
            $pagenum  = $pagenum < 1 ? 1: $pagenum;
            $pagesize = $pagesize < 1 ? 10: $pagesize;
            $start    = ($pagenum - 1) * $pagesize;
            $size     = $pagesize;
            $order    = "user_id desc";
            $where    = [];
            if (!empty($status)) {
                $where = ['status' => $status];
            }

            $sum    = $member->get_count($where);
            $res    = $member->get_list_page($where, $order, $fields = "*", $start, $size);
            $sexarr = [1, '男', '女'];
            foreach ($res as $k => $v) {
                if (!empty($v['sex'])) {
                    $res[$k]['sex'] = $sexarr[$v['sex']];
                }else{
                    $res[$k]['sex'] = "无";
                }
            }

            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $res;

            echo json_encode($data);
            exit;
        }

        return $this->fetch('index');
    }


    // 编辑用户信息
    public function edit()
    {
        $member = new MemberModel();
        $logic  = new MemberLogic();


        $flag    = input("param.flag/s");
        $user_id = input("param.user_id/d");

        // 提交数据处理
        if (request()->isAjax()) {

            // 获取数据
            $data = input("param.");
            
            // 验证信息
            $res = $this->validate($data, \app\admin\validate\User::class);
            if ($res !== true) {
                // 返回错误信息；
                echo json_encode(msg_error($res));
                exit;
            }

            // 添加用户
            $res  = $logic->add_member($data);
            if (isset($res['msg'])) {
                echo json_encode($res);
                exit;
            }

            echo json_encode(msg_success("编辑成功"));
            exit;
        }else{

            if ($flag == "add") {

                return view('add');

            }elseif($flag == "edit"){
                if (empty($user_id)) {
                    $this->error("用户ID不存在");exit;
                }

                // 编辑
                $data = $logic->get_member_info($user_id);
                if (isset($data['msg'])) {
                    $this->error($data['msg']);exit;
                }

                return view('edit', $data);
            }
        }
    }

    // 删除
    public function deluser()
    {
        $member = new MemberModel();
        $id   = input("param.user_id/d");

        if (!empty($id)) {
            $where = ['user_id' => $id];
        }else{
            echo json_encode(msg_error("ID不能不为空"));
            exit;
        }

        // 删除数据
        $res = $member->del_data($where);
        if (isset($res['msg'])) {
            echo json_encode($res);
        }else{
            echo json_encode(msg_success("删除成功"));
        }
        exit;
    }

}
